# -*- coding:utf-8 -*-
'''
example
-----------

from yi01lib.SearchEngine import YahooJapan
from yi01lib.Util import HtmlGen

y=YahooJapan.ImageSearchEngine()
searchResults=y.Search(u"京都")

print HtmlGen.Image(searchResults,u"京都",30)
-----------

'''
from yi01lib.SearchEngine.KeywordSearch import SearchResult

def Normal(SearchResultCollection,q="hogehoge",perpage=20):
    '''
    通常のWeb検索結果のように表示 
    '''
    def showResult(searchResult):
        html ="\n<div class='result'>"
        html+="\n<h2><a href='"+searchResult.URL+"'>"+searchResult.Title.encode("utf-8")+"</a></h2>"
        html+="\n<div class='description'>"+searchResult.Description.encode("utf-8")+"</div>"
        html+="\n<div class='url'>"+searchResult.URL+"</div></div>\n"
        return html
    css='''
div.result{
  margin:1em 2.5%;
  width:100%;
}
div.result h2{
  margin:0px;
  padding:0px;
  font-size:1em;
}
div.result div.description{
  margin:0px;
  padding:0px;
  font-size:0.8em;
}
div.result div.url{
  margin:0px;
  padding:0px;
  font-size:0.8em;
  color:#006600;
}
    '''
    return __show(SearchResultCollection,showResult,css,q,perpage)

def Image(SearchResultCollection,q="hogehoge",imgsize=240,perpage=20):
    '''
    Image検索結果のように表示 
    '''
    imgsizestr=str(imgsize)
    def showResult(searchResult):
        html ="\n<div class='result'>"
        html+="\n<a href='"+searchResult.URL+"' title='"+searchResult.Description.encode("utf-8")+"'><img onload='if(this.width<this.height){this.style.width=("+imgsizestr+"*this.width/this.height)+\"px\"; this.style.height=\""+imgsizestr+"px\";}' src='"+searchResult.URL+"'/></a>"
        html+="\n<div class='subject'>"+searchResult.Title.encode("utf-8")+"</div></div>"
        return html
    css='''
div.result{
  width:'''+str(imgsize+20)+'''px;
  height:'''+str(imgsize+20)+'''px;
  margin:1em;
  float:left;
}
div.result img{
  margin:0px;
  padding:0px;
  width:'''+imgsizestr+'''px;
}
div.result div.description{
  margin:0px;
  padding:0px;
  font-size:0.8em;
}
div.result div.url{
  margin:0px;
  padding:0px;
  font-size:0.8em;
  color:#006600;
}
    '''
    return __show(SearchResultCollection,showResult,css,q,perpage)

def __show(SearchResultCollection,showResultFunc,cssForResults="",q="hogehoge",perpage=20):
    if isinstance(q,unicode): q=q.encode("utf-8")
    N=len(SearchResultCollection)
    totalPage=1+((N-1)/perpage) if N>0 else 0

    html='''
<?php
$curPage=(isset($_GET["page"]))? trim($_GET["page"])-0 : 0;
$perpage='''+str(perpage)+''';
$totalPage='''+str(totalPage)+''';

$searchResults=array();'''
    for searchResult in SearchResultCollection:
        html+="\n$searchResults[]=array(\"Title\"=>\""+searchResult.Title.encode("utf-8")+"\",\"Description\"=>\""+searchResult.Description.encode("utf-8")+"\",\"URL\"=>\""+searchResult.URL.encode("utf-8")+"\");"
    html+='''

$sStart=$curPage*$perpage;
$sEnd=($curPage+1)*$perpage;
if($sEnd>count($searchResults)) $sEnd=count($searchResults);
function showResult($title,$description,$url){
  return "'''+showResultFunc(SearchResult('$title','$description','$url',None)).replace("\"","\\\"")+'''";
}
?>
<html>
<head>
<style>
body div{
  width:100%;
  margin:0px;
  padding:0px;
}
div#head{
  height:4em;
}
div#head div#logo{
  width:5em;
  text-align:center;
  font-family:serif;
  font-size:2em;
  float:left;
}
div#head form{
  padding-top:1em;
  margin-left:5em;
}
div#body{
  clear:both;
}
div#body h1{
  margin:0px;
  padding:0px;
  text-indent:1em;
  font-size:1em;
  height:1.5em;
  border-top:3px solid blue;
  background-color:#ddeeff;
}
div#results{
  width:95%;
}
'''
    html+=cssForResults
    html+='''
div#nextbar{
  clear:both;
  text-align:center;
}
div#nextbar a{
  text-decoration:none;
  font-size:1.5em;
}
div#foot{
  border-top:3px solid blue;
  margin-top:1em;
  padding-top:1em;
  
}
</style>
</head>
<body>

<div id="head">
<div id="logo"><span style="color:red;font-weight:bold;">My</span>Search</div>
<form>
<input type="text" value="'''+q+'''"/>　<input type="submit" value="検索"/>
</form>
</div>

<div id="body">
<h1>'''+q+'''の検索結果 '''+str(N)+'''件中 <? echo $sStart+1; ?>〜<? echo $sEnd; ?>件</h1>
<div id="results">
<?
for($i=$sStart;$i<$sEnd;$i++){
  $s=$searchResults[$i];
  echo showResult($s["Title"],$s["Description"],$s["URL"]);
}
?>
</div>
<div id="nextbar">
<?
if($curPage>0)
  echo '<a href="?page='.($curPage-1).'">&lt;&lt;&lt;</a>';
else
  echo '<span style="color:grey">&lt;&lt;&lt;</span>';
echo "&nbsp;&nbsp;";
for($i=0;$i<$totalPage;$i++){
  echo '<a href="?page='.$i.'">'.($i+1).'</a>';
}
echo "&nbsp;&nbsp;";
if($curPage<$totalPage-1)
  echo '<a href="?page='.($curPage+1).'">&gt;&gt;&gt;</a>';
else
  echo '<span style="color:grey">&gt;&gt;&gt;</span>';

?>
</div>

<div id="foot">
<form>
<input type="text" value="'''+q+'''"/>　<input type="submit" value="検索"/>
</form>
</div>

</body>
</html>'''
    return html